/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package General;

import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class FoUtils {

//    public static ByteArrayOutputStream transformToPdf(String webpath,Source source, String xslContainFoPath) {
//        File fXsl = new File(xslContainFoPath);
//        ByteArrayOutputStream out=new ByteArrayOutputStream();
//        if (source!=null && fXsl.exists()) {
//            try {
//                TransformerFactory tf = TransformerFactory.newInstance();
//                StreamSource xsl = new StreamSource(fXsl);
//                Transformer trans = tf.newTransformer(xsl);
//                StringWriter sw = new StringWriter();
//                StreamResult sr = new StreamResult(sw);
//
//                trans.transform(source, sr);
//
//                FopFactory foFactory= FopFactory.newInstance();
//                FOUserAgent foUserAgent=foFactory.newFOUserAgent();
//                File config=new File(webpath+"WEB-INF/PDF/FOconfig.xconf");
//                foFactory.setUserConfig(config);
//
//                Fop fop=foFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
//                TransformerFactory tfac= TransformerFactory.newInstance();
//                Transformer transformer=tfac.newTransformer();
//
//                //parse string to domsource, string is .fo content generated
//
//                StreamSource xmlStringStream = new StreamSource(new StringReader(sw.toString()));
//               // Source src=new DOMSource(XMLUtils.parseDOMFromString(sw.toString()));
//
//                Result res=new SAXResult(fop.getDefaultHandler());
//
//                transformer.transform(xmlStringStream, res);
//                out.close();
//                return out;
//            } catch (TransformerConfigurationException ex) {
//                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (TransformerException ex) {
//                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            }catch(Exception ex){
//                 Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//        return out;
//    }
//
//     public static ByteArrayOutputStream transformProductToPdf(String webpath,Source source, String xslContainFoPath) {
//        File fXsl = new File(xslContainFoPath);
//        ByteArrayOutputStream out=new ByteArrayOutputStream();
//        if (source!=null && fXsl.exists()) {
//            try {
//                 TransformerFactory tf = TransformerFactory.newInstance();
//                StreamSource xsl = new StreamSource(fXsl);
//                Transformer trans = tf.newTransformer(xsl);
//                StringWriter sw = new StringWriter();
//                StreamResult sr = new StreamResult(sw);
//
//                trans.transform(source, sr);
//
//                FopFactory foFactory= FopFactory.newInstance();
//                FOUserAgent foUserAgent=foFactory.newFOUserAgent();
//                File config=new File(webpath+"WEB-INF/PDF/FOconfig.xconf");
//                foFactory.setUserConfig(config);
//
//                Fop fop=foFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
//                TransformerFactory tfac= TransformerFactory.newInstance();
//                Transformer transformer=tfac.newTransformer(new StreamSource(fXsl));
//
//                //parse string to domsource, string is .fo content generated
//                Result res=new SAXResult(fop.getDefaultHandler());
//                transformer.transform(source, res);
//                out.close();
//                return out;
//            } catch (TransformerConfigurationException ex) {
//                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (TransformerException ex) {
//                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            }catch(Exception ex){
//                 Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//        return out;
//    }
    public static ByteArrayOutputStream createFileFo(String webpath, String xslPath, String xmlPath, Map<String, Object> param, String output) throws FileNotFoundException, SAXException, IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xslStream = new StreamSource(xslPath);
            Transformer trans = tf.newTransformer(xslStream);
            StreamSource xmlStream = new StreamSource(xmlPath);
            StreamResult rs = new StreamResult(new FileOutputStream(output));
            if (param != null) {
                Set<String> keys = param.keySet();
                for (String key : keys) {
                    trans.setParameter(key, param.get(key));
                }
            }
            trans.transform(xmlStream, rs);

            FopFactory foFactory = FopFactory.newInstance();
            Fop fop = foFactory.newFop(MimeConstants.MIME_PDF, out);
            File config = new File(webpath + "WEB-INF/PDF/fop.xconf");
            foFactory.setUserConfig(config);

            TransformerFactory tfac = TransformerFactory.newInstance();
            Transformer transformer = tfac.newTransformer();

            //parse string to domsource, string is .fo content generated
            Source src = new StreamSource(new File(output));

            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
            out.close();
            return out;

        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return out;
    }
}
